﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using FT.DAL;

namespace ShopAdForm.Controls
{
    public partial class DiscountPanel : UserControl
    {
        public DiscountPanel()
        {
            InitializeComponent();
        }

        private DataTable dt=null;
        private int currentIndex = 0;
        private int PrePage = 4;

        private bool HasNext()
        {
            if (currentIndex + PrePage < dt.Rows.Count)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        private bool HasPre()
        {
            return currentIndex > 0;
        }

        public void SetDiscountType(int typeid)
        {
            string sql = "select t.id,t.name,t.oldprice,t.newprice,t.detail,t.backimg,m.ordernum,m.name as shopname"
 +" from navigate_discount_detail t left join navigate_shopes m"
 +" on t.shopid=m.id where t.shopid in(select distinct id from navigate_shopes where type="+typeid+") order by ordernum desc";
            dt = FT.DAL.DataAccessFactory.GetDataAccess().SelectDataTable(sql, "tmp");
            this.panelDiscount.Visible = true;
            if (dt != null )
            {
               // this.panelDiscount.Controls.Clear();
                this.currentIndex = 0;
                this.ShowPageDiscount();
                this.SetPageButtons();
            }
        }

        private void ShowPageDiscount()
        {
            PictureBox pic = null;
            int j = 0;
            for (int i = currentIndex;j<4&& i  < dt.Rows.Count; i++,j++)
            {
                string img=dt.Rows[i]["backimg"]==null?string.Empty:dt.Rows[i]["backimg"].ToString();
                string path = Application.StartupPath + "\\discount\\" + img;
                if (System.IO.File.Exists(path))
                {
                    pic = (PictureBox)this.panelDiscount.Controls["pic" + j.ToString()];
                    if (pic != null)
                    {
                        pic.Image = Image.FromFile(path);
                    }
                }
                else
                {
                    //默认图片
                }
            }
            Button btnTmp = null;
            bool visible = false;
            for (int i = 0; i < 4; i++)
            {
                visible = i <= j;
                pic = (PictureBox)this.panelDiscount.Controls["pic" + i.ToString()];
                pic.Visible = visible;
                btnTmp = (Button)this.panelDiscount.Controls["btnprint" + i.ToString()];

                btnTmp.Visible = visible;
                btnTmp = (Button)this.panelDiscount.Controls["btnnavigate" + i.ToString()];
                btnTmp.Visible = visible;
                btnTmp = (Button)this.panelDiscount.Controls["btndetail" + i.ToString()];
                btnTmp.Visible = visible;
            }
        }

        private void SetPageButtons()
        {
            this.btnPre.Visible = HasPre();
            this.btnNext.Visible = HasNext();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            currentIndex += PrePage;
            this.ShowPageDiscount();
            this.SetPageButtons();

        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            currentIndex -= PrePage;
            this.ShowPageDiscount();
            this.SetPageButtons();
        }

        private void btnnavigate0_Click(object sender, EventArgs e)
        {
            Control ctr = sender as Control;
            int i = Convert.ToInt32(ctr.Tag.ToString());
            string shopname= dt.Rows[currentIndex + i]["shopname"].ToString();
            ShopShowPanel shopShowPanel1 = (ShopShowPanel)this.FindForm().Controls["shopShowPanel1"];
            shopShowPanel1.NavigateShop(shopname);


        }

        private void btnprint0_Click(object sender, EventArgs e)
        {
            Control ctr = sender as Control;
            
        }

        private void btndetail0_Click(object sender, EventArgs e)
        {

        }

        private void DiscountPanel_Load(object sender, EventArgs e)
        {
            this.panelDiscount.Visible = false;
            this.btnNext.Visible = false;
            this.btnPre.Visible = false;
        }
       
    }
}
